package cn.yitun.chat.yitunserverimapicore.business.group.action;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.Info;
import cn.yitun.chat.yituncommonmessage.YitunException;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yituncommonutil.YitunMapUtil;
import cn.yitun.chat.yituncommonutil.YitunStringUtil;
import cn.yitun.chat.yitungeneral.annotation.action.ActionMapping;
import cn.yitun.chat.yitungeneral.annotation.parameter.JsonDefine;
import cn.yitun.chat.yitunnetsession.session.SocketSession;
import cn.yitun.chat.yitunservercommonlib.constant.ActionTypeConstant;
import cn.yitun.chat.yitunservercommonlib.converter.dto2vo.user.UserDto2VoConverter;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.group.GroupMemberDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.group.GroupMemberVo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.system.user.UserVo;
import cn.yitun.chat.yitunservercommonlib.util.NumUtil;
import cn.yitun.chat.yitunservercommonlib.util.R;
import cn.yitun.chat.yitunserverimapicore.business.group.service.GroupCommonService;
import cn.yitun.chat.yitunserverimapicore.business.group.service.GroupMemberService;
import cn.yitun.chat.yitunserverimapicore.business.user.service.UserService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 
 * Date 2019-01-20 21:59:18<br>
 * Description
 * 
 * @author yitun
 * @since 1.0.0
 */
@Component
@ActionMapping(value = ActionTypeConstant.GROUP_MEMBER_ACTION_PUSH)
public class GroupMemberAction {

	@Resource
	private GroupMemberService groupMemberService;
	@Resource
	private GroupCommonService groupCommonService;
	@Resource
	private UserService userService;

	/**
	 * Date 2019-01-23 21:49:05<br>
	 * Description 获取个人已经加入群的数量
	 * @author yitun
	 * @param socketSession
	 * @param query
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0001")
	public ResultMessage getGroupMemberCountOfUserInGroupByUserId(SocketSession socketSession, @JsonDefine("data") GroupMemberDto query) {
		if (NumUtil.isInValid(query.getUserId()))
			query.setUserId(socketSession.getKey());

		return new ResultMessage(R.param("count", groupMemberService.count(YitunMapUtil.java2Map(query,false))));
	}

	/**
	 * Date 2019-01-23 21:49:19<br>
	 * Description 获取个人在已经加入群的关系信息（权限）
	 * @author yitun
	 * @param socketSession
	 * @param query
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0002")
	public ResultMessage getGroupMemberListOfUserInGroupByUserId(SocketSession socketSession, @JsonDefine("data") GroupMemberDto query) {
		query.setUserId(socketSession.getKey());
		List<GroupMemberVo> groupMemberList = groupMemberService.getGroupMemberListOfUserInGroupByUserId(query.getUserId());
		return new ResultMessage(R.param("list", groupMemberList));
	}

	/**
	 * Date 2019-01-23 21:49:35<br>
	 * Description 获取成员数量
	 * @author yitun
	 * @param query
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0003")
	public ResultMessage getGroupMemberCount(@JsonDefine("data") GroupMemberDto query) {
		if (NumUtil.isInValid(query.getGroupId()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		return new ResultMessage(R.param("count", groupMemberService.count(YitunMapUtil.java2Map(query, false))));
	}

	/**
	 * Date 2019-01-23 21:49:45<br>
	 * Description 获取成员关系详情
	 * @author yitun
	 * @param query
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0004")
	public ResultMessage getGroupMemberList(@JsonDefine("data") GroupMemberDto query) {
		if (NumUtil.isInValid(query.getGroupId()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		List<GroupMemberVo> groupMemberList = groupMemberService.getGroupMemberListByGroupId(query.getGroupId());
		return new ResultMessage(R.param("list", groupMemberList));
	}

	/**
	 * Date 2019-01-23 21:49:58<br>
	 * Description 根据成员id获取群成员
	 * @author yitun
	 * @param query
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0005")
	public ResultMessage getGroupMemberById(@JsonDefine("data") GroupMemberDto query) {
		if (NumUtil.isInValid(query.getId()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		return new ResultMessage(R.param("data",  groupMemberService.getVO(query.getId())));
	}

	/**
	 * Date 2019-01-23 21:49:58<br>
	 * Description 根据群组id和用户id获取群成员
	 * @author yitun
	 * @param query
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0006")
	public ResultMessage getGroupMember(@JsonDefine("data") GroupMemberDto query) {
		if (NumUtil.isInValid(query.getGroupId()) || NumUtil.isInValid(query.getUserId()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		return new ResultMessage(R.param("data", groupMemberService.getGroupMemberByGroupIdUserId(query.getGroupId(), query.getUserId())));
	}

	/**
	 * 获取群成员，并修改状态
	 * @param query
	 * @return
	 */
	@ActionMapping(value = "1.1.0007")
	public ResultMessage getGroupUserDataList(@JsonDefine("data") GroupMemberDto query) {
		if (NumUtil.isInValid(query.getGroupId()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		List<UserVo> userDataList = groupCommonService.getUserDataListByGroupMemberGroupId(query.getGroupId());
		userService.setUserStatus(UserDto2VoConverter.INSTANCE.vo2dto(userDataList));
		return new ResultMessage(R.param("list", userDataList));
	}

	/**
	 * 更新群成员昵称
	 * @param socketSession
	 * @param query
	 * @return
	 */
	@ActionMapping(value = "1.1.0010")
	public Object updateRemark(SocketSession socketSession, @JsonDefine("data") GroupMemberDto query) {
		if (NumUtil.isInValid(query.getGroupId()) || NumUtil.isInValid(query.getUserId()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		Long handleUserId = socketSession.getKey();
		Info info = groupMemberService.updateRemark(handleUserId, query.getGroupId(), query.getUserId(), query.getNickname());
		ResultMessage message = new ResultMessage();
		message.setInfo(info);
		return message;
	}

	/**
	 * Date 2019-01-23 21:57:44<br>
	 * Description 设置管理员
	 * @author yitun
	 * @param socketSession
	 * @param query
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0011")
	public Object updatePosition(SocketSession socketSession, @JsonDefine("data") GroupMemberDto query) {
		if (NumUtil.isInValid(query.getGroupId()) || NumUtil.isInValid(query.getUserId())
				|| YitunStringUtil.isEmpty(query.getPosition()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		Long handleUserId = socketSession.getKey();
		Info info = groupMemberService.updatePosition(handleUserId, query.getGroupId(), query.getUserId(), query.getPosition());
		ResultMessage message = new ResultMessage();
		message.setInfo(info);
		return message;
	}

	/**
	 * 
	 * Date 2019-01-23 21:58:11<br>
	 * Description 踢人
	 * 
	 * @author yitun
	 * @param socketSession
	 * @param query
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0012")
	public Object delete(SocketSession socketSession, @JsonDefine("data") GroupMemberDto query) {
		if (NumUtil.isInValid(query.getGroupId()) || NumUtil.isInValid(query.getUserId()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		Long handleUserId = socketSession.getKey();
		Info info = groupMemberService.delete(handleUserId, query.getGroupId(), query.getUserId());
		ResultMessage message = new ResultMessage();
		message.setInfo(info);
		return message;
	}
}
